import type { Rule } from 'ant-design-vue/es/form'

/**
 * 获取用户表单验证规则
 * @param formMode 表单模式（add新增/edit编辑）
 * @returns 表单验证规则对象
 */
export const useUserFormRules = (formMode: Ref<'add' | 'edit'>) => {
  const { t } = useI18n()
  
  return computed<Record<string, Rule[]>>(() => ({
    username: [
      { required: true, message: t('user.usernameRequired'), trigger: 'blur' },
      { min: 4, max: 20, message: t('user.usernameLength'), trigger: 'blur' },
      { pattern: /^[a-zA-Z0-9_]+$/, message: t('user.usernamePattern'), trigger: 'blur' }
    ],
    nickname: [
      { required: true, message: t('user.nicknameRequired'), trigger: 'blur' },
      { min: 2, max: 20, message: t('user.nicknameLength'), trigger: 'blur' },
      { pattern: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/, message: t('user.nicknamePattern'), trigger: 'blur' }
    ],
    password: [
      { required: formMode.value === 'add', message: t('user.passwordRequired'), trigger: 'blur' },
      { min: 6, message: t('user.passwordLength'), trigger: 'blur' },
      { pattern: /[a-zA-Z]/, message: t('user.passwordLetter'), trigger: 'blur' },
      { pattern: /[0-9]/, message: t('user.passwordNumber'), trigger: 'blur' }
    ],
    deptId: [
      { required: true, message: t('user.deptRequired'), trigger: 'change' }
    ],
    phone: [
      { pattern: /^1[3-9]\d{9}$/, message: t('user.phoneInvalid'), trigger: 'blur' }
    ],
    email: [
      { type: 'email', message: t('user.emailInvalid'), trigger: 'blur' }
    ]
  }))
}

/**
 * 获取重置密码表单验证规则
 * @param passwordForm 密码表单对象
 * @returns 重置密码验证规则对象
 */
export const useResetPasswordRules = (passwordForm: { newPassword: string; confirmPassword: string }) => {
  const { t } = useI18n()
  
  return computed<Record<string, Rule[]>>(() => ({
    newPassword: [
      { required: true, message: t('user.newPasswordRequired'), trigger: 'blur' },
      { min: 6, message: t('user.passwordLength'), trigger: 'blur' },
      { pattern: /[a-zA-Z]/, message: t('user.passwordLetter'), trigger: 'blur' },
      { pattern: /[0-9]/, message: t('user.passwordNumber'), trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: t('user.confirmPasswordRequired'), trigger: 'blur' },
      {
        validator: (_: Rule, value: string) => {
          if (value !== passwordForm.newPassword) {
            return Promise.reject(t('user.passwordMismatch'))
          }
          return Promise.resolve()
        },
        trigger: 'blur'
      }
    ]
  }))
} 